﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

namespace GameToolkit
{
    [RequireComponent(typeof(SphereCollider))]
    [AddComponentMenu("Navigation/Runtime NavMeshSphere")]
	public class NavMeshSphereRT : NavMeshSource
    {

        SphereCollider mSphere;
        public override Bounds localBounds
        {
            get
            {
                if (mSphere == null)
                    mSphere = GetComponent<SphereCollider>();
                return new Bounds(mSphere.center, mSphere.radius * Vector3.one);
            }
        }

        public override void GetBuildSource(int defaultArea, ICollection<NavMeshBuildSource> sources)
        {
            SphereCollider sphere;
            if (!TryGetComponent(out sphere))
                return;
            var src = new NavMeshBuildSource();
            src.shape = NavMeshBuildSourceShape.Sphere;
            src.size = sphere.radius * Vector3.one * 2f;
            src.area = this.areaType == 0 ? defaultArea : this.areaType;
            src.transform = transform.localToWorldMatrix * Matrix4x4.Translate(sphere.center);
            sources.Add(src);
        }

//#if UNITY_EDITOR

//        private void OnDrawGizmosSelected()
//        {
//            Gizmos.matrix = transform.localToWorldMatrix;
//            Gizmos.color = Color.gray * 0.8f;
//            var bd = localBounds;
//            Gizmos.DrawWireCube(bd.center, bd.size);
//        }
//#endif
    }
}